

// 获得上传动态的界面
let upPersonDyBox = document.querySelector(".upPersonDy-box");

// 获得个人空间上面的功能按钮
let upDyBtn = document.getElementById("upDyBtn");

// 获得个人动态提交按钮
let submitDyBtn = document.getElementById("submitDyBtn");

// 获取个人动态提交的返回按钮
var trunBackDyBtn = document.querySelector("#trunBackDyBtn");


// 选择是否上传图片
let upDyPhotoBtn = document.getElementById("upDyPhoto");

//  获得浏览上传照片的盒子
let previewDyImgBox = document.getElementById("previewDyImgBox");

// 获得是否保存内容到草稿箱的按钮
let saveBtn = document.getElementById("saveCaoGaoBtn");


// 动态草稿纸
saveBtn.onclick = function(){
    let title = document.getElementById("userDyTitle").value;
    let content = document.getElementById("userDyContent").value;
    window.localStorage.setItem("titleC",title);
    window.localStorage.setItem("contentC",content);
    alert("内容已保存至草稿箱");

}



// 显示上传个人动态界面
upDyBtn.onclick = function () {
    upPersonDyBox.style.display = "block";
    lookPersonDyBox.style.display = "none";
   if(window.localStorage.getItem("titleC")){
    document.getElementById("userDyTitle").value = window.localStorage.getItem("titleC");
    document.getElementById("userDyContent").value = window.localStorage.getItem("contentC");

   }
}


// 预览盒函数
function previewDyImg(allFile){
    let len = allFile.files.length;
    let strImg = '';
        for(let i = 0 ; i<len ; i++){
            strImg += `<img class="previewDyImg" src="./img/blank.png" alt="">`
        }
        previewDyImgBox.innerHTML = strImg;
    for(let i = 0 ; i <len ; i++){
        var reader = new FileReader();
        reader.readAsDataURL(allFile.files[i]);
        reader.onload = function(e){
            var img = document.getElementsByClassName("previewDyImg")[i];
            img.src = e.target.result;
        }
    }  
}




// 为提交个人动态按钮绑定事件
submitDyBtn.onclick = function () {
    // 获取是否上传图片的勾选框
    let upDyPhotoBtn = document.getElementById("upDyPhoto");

    //获取标题 内容 和 图片文件
    let title = document.getElementById("userDyTitle").value;
    let content = document.getElementById("userDyContent").value;
    let ImgFiles = document.getElementById("userDyPhoto");
    
    let changeData = function () {
        let data = new FormData();
        data.append("userId", window.userInfoG.data.id);
        data.append("content", content);
        data.append("title", title);
        //如果选择上传图片
        if (upDyPhotoBtn.checked) {
            data.append("hasPicture", 1);
            //上传多个文件到data
            for(let i = 0 ; i < ImgFiles.files.length;i++){
                data.append("files", ImgFiles.files[i]);
            }
        }
        else {
            //
            data.append("hasPicture", 0);
        }
        if(title && content){
            // 发送AJAX请求
            postForm(data, "dynamicState/insert").then(
                value => {
                    if (value.code == 200) {
                        console.log(value.msg);
                        alert(value.msg);
                        //发布界面消失
                        trunBackDyBtn.click();
                        lookPersonDyBtn.click();
                    }
                    else if (value.code == 500) {
                        console.log(value.msg);
                    }
                    else {
                        console.log(value);
                        alert("后台炸了");
                    }
                },
                reason => {
                    alert("发送AJAX请求失败");
                }
            )

        }
        else{
            alert("请输入完整数据");
        }
    }
    changeData();

}
// 返回按钮
trunBackDyBtn.onclick = function () {
    //上传动态的界面消失
    upPersonDyBox.style.display = "none";
}

